package com.starblink.android.common.web

import android.app.Activity
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.common.databinding.ItemWebDealCodeBinding
import com.starblink.android.common.databinding.PopWebviewDealBinding
import com.starblink.android.common.web.deal.WebDealCell

object WebDealPopView {

    private var popupWindow: PopupWindow? = null
    fun showPop(
        context: Activity,
        parent: ViewGroup,
        anchorView: View,
        dealList: List<WebSuperDealData>
//        product: ProductF,
//        position: Int
    ): PopupWindow? {
        val viewBinding =
            PopWebviewDealBinding.inflate(LayoutInflater.from(context), null, false)
        popupWindow = PopupWindow(
            viewBinding.root,
            ViewGroup.LayoutParams.MATCH_PARENT,
//            ScreenUtils.getRawScreenSize(context)[1],
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true
        )

        viewBinding.recyclerView.layoutManager = LinearLayoutManager(context)
        viewBinding.recyclerView.setHasFixedSize(true)
        viewBinding.recyclerView.itemAnimator = null
        val adapter: MutableAdapter by lazyOnNone {
            MutableAdapter(dealList).apply {
                addVhDelegate {
                    WebDealCell(ItemWebDealCodeBinding.inflate(it.inflater, it, false))
                }
            }
        }

        viewBinding.recyclerView.adapter = adapter
        //覆盖状态栏
        popupWindow?.isFocusable = false
        popupWindow?.isOutsideTouchable = false

        popupWindow?.showAtLocation(parent, Gravity.BOTTOM, 0, 69.dp2px())
//            popWindowView.llSimilar.trackData(
//                element = SpmElementDef.POP_FIND_SIMILAR_CLICK,
//                page = pageName, onlyHit = true,
//                traceInfo = product.traceInfo,
//                params = trackProductMap(position, product)
//            )

        return popupWindow
    }

}